package com.eastfair.matching.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.request.PageParams;
import com.eastfair.common.dto.AttachmentFileDTO;
import com.eastfair.core.exception.BizException;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.matching.dao.ComplementServiceItemMapper;
import com.eastfair.matching.dto.ComplementServiceItemPageQuery;
import com.eastfair.matching.dto.ComplementServiceItemSaveDTO;
import com.eastfair.matching.dto.ComplementServiceItemUpdateDTO;
import com.eastfair.matching.entity.ComplementServiceItem;
import com.eastfair.matching.service.ComplementServiceItemService;
import com.eastfair.boot.service.SuperServiceImpl;

import com.eastfair.matching.vo.ComplementServiceItemVO;
import com.eastfair.pay.api.AttachmentFileFeign;
import com.eastfair.projectcore.enumeration.DataIsolationSystemSceneEnum;
import com.eastfair.venuebooking.api.ServiceItemFeign;
import com.eastfair.venuebooking.entity.Space;
import com.eastfair.venuebooking.vo.SpaceVO;
import com.eastfair.venueservice.dto.ServiceItemDTO;
import com.eastfair.venueservice.dto.ServiceItemQuery;
import com.eastfair.venueservice.entity.ServiceAuditRecordDetail;
import com.eastfair.venueservice.vo.ServiceItemVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;

import java.util.Collection;
import java.util.Objects;

//import com.baomidou.dynamic.datasource.annotation.DS;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 * 业务实现类
 * 服务项
 * </p>
 *
 * @author lyw
 * @date 2022-07-15
 */
@Slf4j
@Service
public class ComplementServiceItemServiceImpl extends SuperServiceImpl<ComplementServiceItemMapper, ComplementServiceItem> implements ComplementServiceItemService {
    @Autowired
    private ServiceItemFeign serviceItemFeign;


    /**
     * 处理逻辑删除相关处理
     *
     * @param modelList 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerRemoveByIdsOfLogic(Collection<ComplementServiceItem> modelList) {
        modelList.stream().forEach(s -> {
            s.setIsDeleted(BusinessConstant.YES);
        });
        // TODO: 2021/10/29 这里需要操作其他业务，如清空角色等
        return R.successDef();
    }

    /**
     * 配套服务项列表
     *
     * @param params
     * @return
     */
    @Override
    public IPage<ServiceItemVO> queryComplementServiceItemList(PageParams<ServiceItemQuery> params) {
        log.info("ComplementServiceItemServiceImpl +++++ queryComplementServiceItemList - 配套服务项列表 params={}", JSONObject.toJSON(params));
        params.getModel().setServiceProviderType(DataIsolationSystemSceneEnum.P_COMPOSITE_MATING.getCode());
        IPage<ServiceItemVO> re = params.buildPage();
        R<IPage<ServiceItemVO>> r = (R<IPage<ServiceItemVO>>) serviceItemFeign.queryServiceItem(params);
        if(!r.getIsSuccess()){
            throw BizException.wrap(r.getCode(),r.getMsg());
        }
        BeanUtil.copyProperties(r.getData(), re);
        return re;
    }

    /**
     * 保存配套服务项
     *
     * @param serviceItemDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveComplementServiceItemInfo(ServiceItemDTO serviceItemDTO) {
        //最小预定量复初始值
        serviceItemDTO.setMinAllocatedNum(0);
        serviceItemDTO.setServiceProviderType(DataIsolationSystemSceneEnum.P_COMPOSITE_MATING.getCode());
        log.info("ComplementServiceItemServiceImpl +++++ saveComplementServiceItemInfo - 保存配套服务项 params={}", JSONObject.toJSON(serviceItemDTO));
        R<Boolean> r = serviceItemFeign.addServiceItem(serviceItemDTO);
        if(!r.getIsSuccess()){
            throw BizException.wrap(r.getCode(),r.getMsg());
        }
        return r.getData();
    }

    /**
     * 修改配套服务项
     * @param serviceItemDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateComplementServiceItemById(ServiceItemDTO serviceItemDTO) {
        serviceItemDTO.setMinAllocatedNum(0);
        serviceItemDTO.setServiceProviderType(DataIsolationSystemSceneEnum.P_COMPOSITE_MATING.getCode());
        log.info("ComplementServiceItemServiceImpl +++++ updateComplementServiceItemById - 修改配套服务项 params={}", JSONObject.toJSON(serviceItemDTO));
        R<Boolean> r = serviceItemFeign.updateServiceItem(serviceItemDTO);
        if(!r.getIsSuccess()){
            throw BizException.wrap(r.getCode(),r.getMsg());
        }
        return r.getData();
    }

    /**
     * 上架配套服务
     *
     * @param serviceItemDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean shelfService(ServiceItemDTO serviceItemDTO) {
        log.info("ComplementServiceItemServiceImpl +++++ shelfService - 上架配套服务 params={}", JSONObject.toJSON(serviceItemDTO));
        R<Boolean> r = serviceItemFeign.shelfService(serviceItemDTO);
        if(!r.getIsSuccess()){
            throw BizException.wrap(r.getCode(),r.getMsg());
        }
        return r.getData();
    }

    /**
     * 下架配套服务
     *
     * @param serviceItemDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean offShelfService(ServiceItemDTO serviceItemDTO) {
        log.info("ComplementServiceItemServiceImpl +++++ offShelfService - 下架配套服务 params={}", JSONObject.toJSON(serviceItemDTO));
        R<Boolean> r = serviceItemFeign.offShelfService(serviceItemDTO);
        if(!r.getIsSuccess()){
            throw BizException.wrap(r.getCode(),r.getMsg());
        }
        return r.getData();
    }

    /**
     * 删除配套服务
     *
     * @param serviceItemDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delComplementServiceItemById(ServiceItemDTO serviceItemDTO) {
        log.info("ComplementServiceItemServiceImpl +++++ offShelfService - delComplementServiceItemById params={}", JSONObject.toJSON(serviceItemDTO));
        R<Boolean> r = serviceItemFeign.delServiceItem(serviceItemDTO);
        if(!r.getIsSuccess()){
            throw BizException.wrap(r.getCode(),r.getMsg());
        }
        return r.getData();
    }


}
